<?php
/**
 * 同意催（催款，催签）操作
 * User: Administrator
 * Date: 2018/8/6/006
 * Time: 20:33
 */

namespace app\lib\Order\OrderOption;


use app\lib\Order\IsOrder\IsOrder;
use app\lib\Order\OrderExtensionConfig\OrderBindingExtension;
use app\lib\Order\OrderInfo\GetOrderMoney;
use app\lib\Order\OrderMoney\AddOrderMoneyRecord;
use app\lib\Order\OrderPaymentNotice\UpdateOrderNotice;
use app\lib\Order\OrderSign\AddOrderSignRecord;
use app\model\OrderPaymentNoticeModel;
use app\model\OrderSignRecordModel;

class AgreeOrderUrgeMoneyOption
{
    protected $noticeId;
    protected $noticeInfo;
    protected $opUid;

    /**
     * 初始化
     * AgreeOrderUrgeMoneyOption constructor.
     * @param $noticeId
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function __construct($noticeId,$opUid)
    {
        $this->noticeId = $noticeId;
        $model =  new OrderPaymentNoticeModel();
        $map  = [$model->_id=>$this->noticeId];
        $field = [$model->_id,$model->_orderId,$model->_moneyId,$model->_type,$model->_status];
        $info =  $model->where($map)->field($field)->find();
        if(empty($info)){
            exception('未获取通知申请');
        }
        $this->noticeInfo =  $info;
        $this->opUid = $opUid;
    }

    /**
     * 验证
     * @return bool
     * @throws \Exception
     */
    protected function check(){
        $model =  (new IsOrder($this->noticeInfo['orderId']));
        /*判断工单是否进行中*/
        if(!$model->isExecuteOrder()){
            exception('工单状态不在进行中');
        };
        return true;
    }

    /**
     * 工单同意催款后处理
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function handlePay(){
        /*判断是否存在未交款记录*/
        $moneyModel  =  (new GetOrderMoney($this->noticeInfo['orderId'],false));
        $moneyModel->setMoneyId($this->noticeInfo['moneyId']);
        $moneyRecord =  $moneyModel->getFindInfo();
        /*不存在*/
        if($moneyRecord['status'] == null){
            /*生成待交款信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($moneyRecord['id'])->noPay();
            /*挂上未交款标签*/
            (new OrderBindingExtension($this->noticeInfo['orderId'],$this->opUid,2))->save();
        }
        /*发生通知*/

        /*记录工单日志*/
        $this->writingOrderLog('DUNNING','工单id为'.$this->noticeInfo['orderId'].'进行了催单操作');
        return true ;
    }

    /**
     * 工单同意催签后操作
     * @return bool
     * @throws \Exception
     */
    protected function handleSign(){
        /*判断有没有催签记录*/
        $model =  (new OrderSignRecordModel());
        $map = [$model->_orderId=>$this->noticeInfo['orderId'],$model->_status=>0];
        $id =  $model->where($map)->value($model->_id);
        /*没有生成催签记录*/
        if(empty($id)){
            (new AddOrderSignRecord($this->noticeInfo['orderId'],$this->opUid))->save();
        }
        /*记录工单日志*/
        $this->writingOrderLog('REMINDERSIG','工单id为'.$this->noticeInfo['orderId'].'进行了催单操作');
        return true;
    }
    /**
     * 执行代码
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save(){
        switch ($this->noticeInfo['type']){
            case 'pay':
                $this->handlePay();
                break;
            case 'sign':
                $this->handleSign();
                break;
        }
        /*同意操作*/
        $noticeModel = new UpdateOrderNotice($this->noticeId);
        $noticeModel->setOpUid($this->opUid);
        $noticeModel->agree();
    }

    /**
     * 记录工单日志
     * @param $type
     * @param $content
     * @return bool
     */
    protected function writingOrderLog($type,$content){
        $log =  new  OrderOptionLog($this->noticeInfo['orderId']);
        $log->setOptionType($type);
        $log->setOptionUserId($this->opUid);
        $log->setSubmitParameter('');
        $log->setOptionContent($content);
        $log->save();
        return true;
    }
}